/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.card.service.impl;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.card.mapper.CardIntroductionRelationMapper;
import top.continew.admin.card.model.entity.CardIntroductionRelationDO;
import top.continew.admin.card.model.query.CardIntroductionRelationQuery;
import top.continew.admin.card.model.req.CardIntroductionRelationReq;
import top.continew.admin.card.model.resp.CardIntroductionRelationDetailResp;
import top.continew.admin.card.model.resp.CardIntroductionRelationResp;
import top.continew.admin.card.service.CardIntroductionRelationService;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * 微信名片公司简介关系业务实现
 *
 * @author floyd
 * @since 2024/12/02 16:06
 */
@Service
@RequiredArgsConstructor
public class CardIntroductionRelationServiceImpl extends BaseServiceImpl<CardIntroductionRelationMapper, CardIntroductionRelationDO, CardIntroductionRelationResp, CardIntroductionRelationDetailResp, CardIntroductionRelationQuery, CardIntroductionRelationReq> implements CardIntroductionRelationService {

    @Override
    public void addList(CardIntroductionRelationReq req) {

        //        var list = req.getIntroductionIds();
        //        list.forEach(item -> {
        //            CardIntroductionRelationDO model = new CardIntroductionRelationDO();
        //            model.setCardId(req.getCardId());
        //            model.setIntroductionId(item);
        //
        //            baseMapper.insert(model);
        //        });
    }

    @Override
    public List<CardIntroductionRelationDO> getListByCardId(Long cardId) {
        return baseMapper.lambdaQuery().in(CardIntroductionRelationDO::getCardId, cardId).list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(List<Long> introductionIds, Long cardId) {
        // 检查是否有变更
        List<Long> oldIdList = baseMapper.lambdaQuery()
            .select(CardIntroductionRelationDO::getIntroductionId,CardIntroductionRelationDO::getCardId)
            .eq(CardIntroductionRelationDO::getCardId, cardId)
            .list()
            .stream()
            .map(CardIntroductionRelationDO::getIntroductionId)
            .toList();
        if (CollUtil.isEmpty(CollUtil.disjunction(introductionIds, oldIdList))) {
            return false;
        }
        // 删除原有关联
        baseMapper.lambdaUpdate().eq(CardIntroductionRelationDO::getCardId, cardId).remove();
        // 保存最新关联
        List<CardIntroductionRelationDO> relationList = introductionIds.stream()
            .map(introductionId -> new CardIntroductionRelationDO(cardId, introductionId))
            .toList();
        return baseMapper.insertBatch(relationList);
    }
}